package zhangqi.com.mymp3;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.ListView;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class MusicService extends Service {

    private MediaPlayer player;
    private Timer timer;
    @Override
    public void onCreate() {
        super.onCreate();
        player = new MediaPlayer();
    }

    private int position;

    /*@Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        position = intent.getIntExtra("position", -1);
        Log.i("zhangqi","在service中的position的位置是"+position);
    }*/

    @Override
    public int onStartCommand(Intent intent2, int flags, int startId) {
        position = intent2.getIntExtra("position", -1);
        Log.i("zhangqi","在service中的intent2的值是"+intent2);
        Log.i("zhangqi","在service中的position的位置是"+position);
        return super.onStartCommand(intent2, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        player.stop();
        player.release();
        player =null;
        if(timer==null){
            timer.cancel();
            timer = null;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MusicController();
    }

    class MusicController extends Binder implements MusicInterface{

        @Override
        public void play() {
            MusicService.this.play();
        }

        @Override
        public void continuePlay() {
            MusicService.this.continuePlay();
        }

        @Override
        public void pause() {
            MusicService.this.pause();
        }

        @Override
        public void seekTo(int progress) {
            MusicService.this.seekTo(progress);
        }
    }

    public void play(){

        player.reset();
        try {
            String[] songs = new String[]{"/sdcard/mp3/a.mp3","/sdcard/mp3/b.mp3","/sdcard/mp3/c.mp3","/sdcard/mp3/d.mp3","/sdcard/mp3/e.mp3"};

            //int p = Integer.parseInt(position);
            player.setDataSource(songs[position]);


            player.prepareAsync();
            player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    player.start();
                    addTimer();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public void continuePlay(){
        player.start();
    }
    public void pause(){
        player.pause();
    }
    public void seekTo(int progress){
        player.seekTo(progress);
    }

    public void addTimer(){
        if(timer==null){
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    int duration =player.getDuration();
                    int currentPosition = player.getCurrentPosition();
                    Message msg = MainActivity.handler.obtainMessage();
                    Bundle bundle = new Bundle();
                    bundle.putInt("duration",duration);
                    bundle.putInt("currentPosition",currentPosition);
                    msg.setData(bundle);
                    MainActivity.handler.sendMessage(msg);
                }
            },5,500);
        }
    }

}
